package bear;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

public class Sequencer
{
	private ArrayList<Chat> hystory;
	private HashMap<String, HashMap<String, ArrayList<Chat>>> historySequence;//HashMap<Nick, HashMap<NickIncipit, ArrayList<Chat>>>

	public Sequencer(ArrayList<Chat> hystory)
	{
		this.hystory=hystory;
	}

	public static void main(String[] args)
	{
		Chat[] hystory = new Chat[] {
				new Chat("", "","aa1", "n1"),
				new Chat("aa1", "aa2","aa3", "n1"),
				new Chat("aa3", "aa4","aa5", "n1"),
				new Chat("aa5", "aa6","aa7", "n1"),
				new Chat("aa7", "aa8","aa9", "n1"),
				new Chat("aa9", "a11","a12", "n1"),
				new Chat("a12", "a13","a14", "n1"),
				
				new Chat("", "","bb1", "n1"),
				new Chat("bb1", "bb2","bb3", "n1"),
				new Chat("bb5", "bb6","bb7", "n1"),
				new Chat("bb3", "bb4","bb5", "n1"),
				
				new Chat("", "","cc1", "n1"),
				new Chat("cc1", "cc2","cc3", "n1"),
				new Chat("cc3", "cc4","cc5", "n1"),
				new Chat("cc5", "cc6","cc7", "n1"),
				new Chat("cc7", "cc8","cc9", "n1"),
				
				new Chat("", "","dd1", "n1"),
				new Chat("dd1", "dd2","dd3", "n1"),
				new Chat("dd3", "dd4","dd5", "n1"),
				
				new Chat("", "","dd1", "n2"),
				new Chat("dd1", "dd2","dd3", "n2"),
				new Chat("dd3", "dd4","dd5", "n2"),
				
				new Chat("", "","cc1", "n2"),
				new Chat("cc1", "cc2","cc3", "n2"),
				new Chat("cc3", "cc4","cc5", "n2"),
				new Chat("cc5", "cc6","cc7", "n2"),
				new Chat("cc7", "cc8","cc9", "n2"),
				

				new Chat("", "","a11", "n3"),
				new Chat("aa1", "aa2","aa3", "n3"),
				new Chat("aa3", "aa4","aa5", "n3"),
				new Chat("aa5", "aa6","aa7", "n3"),
				new Chat("aa7", "aa8","aa9", "n3"),
				new Chat("aa9", "a11","a12", "n3"),
				new Chat("a12", "a13","a14", "n3"),
				

				new Chat("", "","cc1", "n3"),
				new Chat("cc1", "cc2","cc3", "n3"),
				new Chat("cc3", "cc4","cc5", "n3"),
				new Chat("cc5", "cc6","cc7", "n3"),
				
				new Chat("", "cc0","cc1", "n4"),
				new Chat("cc1", "cc2","cc3", "n4"),
				new Chat("cc3", "cc4","cc5", "n4"),
				new Chat("cc5", "cc6","cc7", "n4")
		};
		
		ArrayList<Chat> h= new ArrayList<Chat>();
		Collections.addAll(h, hystory);
		Sequencer sequencer = new Sequencer(h);
		sequencer.process();
		
		HashMap<String, HashMap<String, ArrayList<Chat>>> hystorySequence = sequencer.getHistorySequence();
		print(hystorySequence);
	}

	private static void print(HashMap<String, HashMap<String, ArrayList<Chat>>> historySequence)
	{
		for(String nick: historySequence.keySet()){
			System.out.println("\n\nNick: "+ nick);
			for(String incipit : historySequence.get(nick).keySet()){
				System.out.println("\t\tIncipit: "+ incipit);
				ArrayList<Chat> thread=historySequence.get(nick).get(incipit);
				for(Chat c :  thread){
					System.out.println("\t\t\t\t"+c);
				}
			}
		}
		
	}

	private HashMap<String, HashMap<String, ArrayList<Chat>>> getHistorySequence()
	{
		return historySequence;
	}

	private void process()
	{
		historySequence= new HashMap<String, HashMap<String,ArrayList<Chat>>>();
		//sort history by chat
		HashMap<String, ArrayList<Chat>> intermediate01 = new HashMap<String, ArrayList<Chat>>();
		for(Chat c : hystory){
			ArrayList<Chat> list = intermediate01.get(c.nick);
			if(list==null)
				list = new ArrayList<Chat>();
			list.add(c);
			intermediate01.put(c.nick, list);
		}
		
		for(String nick : intermediate01.keySet()){
			
			//find incipit
			ArrayList<Chat> list = intermediate01.get(nick);
			ArrayList<Integer> incipit = new ArrayList<Integer>();
			for(int i=0;i<list.size(); i++)
			{
				Chat c = list.get(i);
				if(c.mio.equalsIgnoreCase(""))
					incipit.add(i);
			}

			//create Thread for each incipit
			HashMap<String, ArrayList<Chat>> intermediate02 = new HashMap<String, ArrayList<Chat>>();
			for(int index : incipit)
			{
				
				boolean continueLoop=true;
				Chat start=list.get(index);
				String incipitString ="["+start.suo+"] [" + start.reply+"]";
				ArrayList<Chat> thread = intermediate02.get(incipitString);
				if(thread==null);
					thread = new ArrayList<Chat>();
				while (continueLoop)
				{
					continueLoop=false;
					for(Chat c :list)
					{
						if(start.reply.equalsIgnoreCase(c.mio))
						{
							continueLoop=true;
							thread.add(c);
							start=c;
							break;
						}
					}				
					
				}
				intermediate02.put(incipitString, thread);				
			}
			
			historySequence.put(nick, intermediate02);
		}
		
	}
}
